package com.think.teaching.controller;


import com.think.common.controller.BaseController;
import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.CourseComment;
import com.think.teaching.service.impl.CourseCommentServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程评论 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/v1")
@Api(tags = "课程评论接口文档")
public class CourseCommentController extends BaseController{
	
	private final CourseCommentServiceImpl courseCommentService;

	public CourseCommentController(CourseCommentServiceImpl courseCommentService) {
		this.courseCommentService = courseCommentService;
	}

	@ApiOperation("用户添加新的课程评论")
	@PostMapping("user/courseComment")
	public Result AddNewCourseComment(@RequestBody()CourseComment data) {
		Result result = data.CheckObjectComplete();
		if(result.getCode() == ResultCode.SUCCESS.code()) {
			if ( data.getReplyTo() != null && data.getReplyTo()>0 &&courseCommentService.getById(data.getReplyTo()) == null) {
				return Result.failure(ResultCode.COURSE_COMMENT_NOT_EXIST);
			}
			if (data.getContent()!=null && data.getContent().length() ==0) {
				return Result.failure(ResultCode.COURSE_COMMENT_NO_CONTENT);
			}
			CourseComment courseComment = courseCommentService.AddCourseComment(data);
			if( courseComment!= null) {
				return Result.success(courseComment);
			}
			else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
		else
			return result;
	}
	
//	@ApiOperation("用户更新课程评论")
//	@RequestMapping(value = "user/courseComment",method = RequestMethod.PUT)
//	public Result UpdateCourseComment(@RequestBody()CourseComment data) {
//		Result result = data.CheckObjectComplete();
//		if(result.getCode() == ResultCode.SUCCESS.code()) {
//			CourseComment courseComment = courseCommentService.UpdateCourseComment(data);
//			if( courseComment!= null) {
//				return Result.success(courseComment);
//			}
//			else {
//				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
//			}
//		}
//		else
//			return result;
//	}
	
	@ApiOperation("管理员删除课程评论")
	@RequestMapping(value = "admin/courseComment/{id}",method = RequestMethod.DELETE)
	public Result DeleteCourseComment(@PathVariable("id")Integer id) {
		CourseComment courseComment = courseCommentService.getById(id);
		if( courseComment== null) {
			return Result.failure(ResultCode.COURSE_COMMENT_NOT_EXIST);
		}
		else {
			if (courseCommentService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}
	
	@ApiOperation("管理员获取课程评论详情")
	@RequestMapping(value = "admin/courseComment/{id}",method = RequestMethod.GET)
	public Result GetCourseComment(@PathVariable("id")Integer id) {
		CourseComment courseComment = courseCommentService.getById(id);
		if( courseComment== null) {
			return Result.failure(ResultCode.COURSE_COMMENT_NOT_EXIST);
		}
		else {
			
			return Result.success(courseComment);
		}
	}
	
	@ApiOperation("管理员修改课程评论状态")
	@RequestMapping(value = "admin/courseComment/{id}/{state}",method = RequestMethod.PUT)
	public Result UpdateCourseCommentState(@PathVariable("id")Integer id,@PathVariable("state")Integer state) {
		CourseComment courseComment = courseCommentService.getById(id);
		if( courseComment== null) {
			return Result.failure(ResultCode.COURSE_COMMENT_NOT_EXIST);
		}
		else {
			try {
				courseComment = courseCommentService.SetCourseCommentState(id, state);
				return Result.success(courseComment);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}
	
	@ApiOperation("用户搜索课程评论")
	@RequestMapping(value = "user/courseComment/search",method = RequestMethod.GET)
	public Result UserSearchCourseComment(@RequestParam("key")String key) {
		return Result.success(courseCommentService.SearchCourseCommentsWithTag(key, true));
	}
	
	@ApiOperation("管理员搜索课程评论")
	@RequestMapping(value = "admin/courseComment/search",method = RequestMethod.GET)
	public Result AdminSearchCourseComment(@RequestParam("key")String key) {
		return Result.success(courseCommentService.SearchCourseCommentsWithTag(key, false));
	}
	
//	@ApiOperation("管理员获取课程评论列表")
//	@RequestMapping(value = "admin/courseComments/{page}/{count}",method = RequestMethod.GET)
//	public Result AdminListCourseComment(@PathVariable("page")Integer page,@PathVariable("count")Integer count,@RequestParam("tag")Integer tag,
//			Integer type) {
//		return Result.success(courseCommentService.ListCourseCommentWithTagAndType(count, page, type, tag, false));
//	}
//	
	@ApiOperation("用户获取课程评论列表")
	@RequestMapping(value = "open/courseComments/{page}/{count}",method = RequestMethod.GET)
	public Result UserListCourseComment(@PathVariable("page")Integer page,@PathVariable("count")Integer count,@RequestParam("id")Integer id) {
		return Result.success(courseCommentService.ListCourseCommentWithTagAndType(count, page, id));
	}
}

